package com.arges.web.serviceImpl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Order;
import org.springframework.transaction.annotation.Transactional;

import com.arges.web.common.EnhancedBeanUtils;
import com.arges.web.common.QueryColumn;
import com.arges.web.dao.GenericDao;
import com.arges.web.domain.tableutil.DbTableForDataDelete;
import com.arges.web.service.GenericManage;

/**
 * servic层公用类接口实现类
 * 
 * @author arges
 * 
 * @param <Pojo>
 * @param <PK>
 */
@Transactional
public abstract class GenericManageImpl<Pojo, Bean> implements GenericManage<Pojo, Bean>
{
	public abstract GenericDao<Pojo, Long> getGenericDao();

	@Override
	public void batchSave(Collection<? extends Object> collection)
	{
		getGenericDao().batchSave(collection);
	}

	@Override
	public void batchSaveOrUpdate(Collection<? extends Object> collection)
	{
		getGenericDao().batchSaveOrUpdate(collection);
	}

	@Override
	public void delete(Pojo object)
	{
		getGenericDao().delete(object);
	}

	@Override
	public List<Pojo> findAll()
	{
		return getGenericDao().findAll();
	}

	@Override
	public void save(Pojo object)
	{
		getGenericDao().save(object);
	}

	@Override
	public void saveOrUpdate(Pojo object)
	{
		getGenericDao().saveOrUpdate(object);
	}

	@Override
	public void update(Pojo object)
	{
		getGenericDao().update(object);
	}

	@Override
	public void delete(Long id)
	{
		getGenericDao().delete(id);
	}

	@Override
	public Pojo get(Long id)
	{
		return getGenericDao().get(id);
	}

	// @Override
	// public List<DictionaryBean> getDictionaryList(String dictionaryType, String param1, String param2)
	// {
	// List<Dictionary> dictionarys = getGenericDao().getDictionaryList(dictionaryType, param1, param2);
	//
	// return EnhancedBeanUtils.createAndCopyToList(dictionarys, DictionaryBean.class);
	// }
	//
	// @Override
	// public List<DictionaryBean> getDictionaryList(String dictionaryType, List<Criterion> conditions)
	// {
	// List<Dictionary> dictionarys = getGenericDao().getDictionaryList(dictionaryType, conditions);
	// return EnhancedBeanUtils.createAndCopyToList(dictionarys, DictionaryBean.class);
	// }

	@Override
	public void batchDelete(List<DbTableForDataDelete> dbTables)
	{
		for (DbTableForDataDelete dbTable : dbTables)
		{
			getGenericDao().executeSql(dbTable.toSql());
		}
	}

	@Override
	@SuppressWarnings("unchecked")
	public List findByCriterions(List<Criterion> conditions, Class beanClass, Class pojoClass)
	{
		return findByCriterions(conditions, beanClass, pojoClass, null);
	}

	@Override
	@SuppressWarnings("unchecked")
	public List findByCriterions(List<Criterion> conditions, Class beanClass, Class pojoClass,
			List<QueryColumn> queryCols)
	{
		return findByCriterions(conditions, beanClass, pojoClass, queryCols, true);
	}

	@Override
	@SuppressWarnings("unchecked")
	public List findByCriterions(List<Criterion> conditions, Class beanClass, Class pojoClass,
			List<QueryColumn> queryCols, boolean toObj)
	{
		return findByCriterions(conditions, beanClass, pojoClass, queryCols, toObj, null);
	}

	@SuppressWarnings("unchecked")
	public List findByCriterions(List<Criterion> conditions, Class beanClass, Class pojoClass,
			List<QueryColumn> queryCols, boolean toObj, List<Order> orders)
	{
		List result = null;
		try
		{
			List pojoList = getGenericDao().findByConditions(conditions, pojoClass, queryCols, toObj, orders);
			if (beanClass == null)
			{
				result = pojoList;
			}
			else
			{
				result = EnhancedBeanUtils.createAndCopyToList(pojoList, beanClass);
			}
		}
		catch (Exception e)
		{
		}
		return result;
	}

	@Override
	@SuppressWarnings("unchecked")
	public List findBySingleCondition(Criterion condition, Class beanClass, Class pojoClass)
	{
		return findBySingleCondition(condition, beanClass, pojoClass, null);
	}

	@Override
	@SuppressWarnings("unchecked")
	public List findBySingleCondition(Criterion condition, Class beanClass, Class pojoClass, List<QueryColumn> queryCols)
	{
		return findBySingleCondition(condition, beanClass, pojoClass, queryCols, true);
	}

	@Override
	@SuppressWarnings("unchecked")
	public List findBySingleCondition(Criterion condition, Class beanClass, Class pojoClass,
			List<QueryColumn> queryCols, boolean toObj)
	{
		List<Criterion> conditions = new ArrayList<Criterion>();
		conditions.add(condition);
		return findByCriterions(conditions, beanClass, pojoClass, queryCols, toObj);
	}
}
